package com.seeyon.apps.kk.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.seeyon.apps.collaboration.manager.ColManager;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.kk.manager.EditOpinionsManagerImpl;
import com.seeyon.apps.kk.manager.OldDataManagerImpl;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.kk.utils.CAP4FormKitUtils;

import jersey.repackaged.com.google.common.collect.Maps;

/**
 * 意见区编辑Ctr
 * 
 * @author Administrator
 *
 */
public class EditOpinionsController extends KkBaseController {
	private final Log log = LogFactory.getLog(EditOpinionsController.class);
	private EditOpinionsManagerImpl editOpinionsManagerImpl;
	private OldDataManagerImpl oldDataManagerImpl;

	// kk 获取表单API start
	private CAP4FormManager formManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
	private ColManager colManager = (ColManager) AppContext.getBean("colManager");
	// kk end

	public OldDataManagerImpl getOldDataManagerImpl() {
		return oldDataManagerImpl;
	}

	public void setOldDataManagerImpl(OldDataManagerImpl oldDataManagerImpl) {
		this.oldDataManagerImpl = oldDataManagerImpl;
	}

	public EditOpinionsManagerImpl getEditOpinionsManagerImpl() {
		return editOpinionsManagerImpl;
	}

	public void setEditOpinionsManagerImpl(EditOpinionsManagerImpl editOpinionsManagerImpl) {
		this.editOpinionsManagerImpl = editOpinionsManagerImpl;
	}

	public ModelAndView postEditOpinion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/saveCaseStatus");
		Map<String, Object> map = new HashMap<>();
		map.put("CTPID", request.getParameter("formID"));
		map.put("CONTENT", request.getParameter("CONTENT").equals("") ? null : request.getParameter("CONTENT"));
		mav.addObject("status", editOpinionsManagerImpl.updateOpinion(map));
		return mav;
	}

	public ModelAndView dataSync(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (this.oldDataManagerImpl.start())
			return this.ajaxDoneSuccess("历史数据同步成功", JSON.toJSONString("200"));
		else
			return this.ajaxDoneError("历史数据同步失败", JSON.toJSONString("500"));
	}

	public ModelAndView getOldDataByFielNo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/catOldData");
		String fielNo = request.getParameter("fielNo");
		Map<String, Object> map = oldDataManagerImpl.getOldDataByFielNo(fielNo);
		request.setAttribute("rMap", map);
		return mav;
	}

	/**
	 * 撤销后 清除表单 审批人、抄送人
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void removeTableContext(HttpServletRequest request, HttpServletResponse response) {
		String summaryId = request.getParameter("summaryId");
		try {
			ColSummary summary = colManager.getColSummaryById(Long.valueOf(summaryId)); // 获取协同信息
			CtpTemplate template = templateManager.getCtpTemplate(summary.getTempleteId());
			FormBean formBean = formManager.getFormByFormCode(template);
			FormDataMasterBean formMasterBean = formManager.getDataMasterBeanById(summary.getFormRecordid(), formBean,
					null);
			FormFieldBean csTFfb = formMasterBean.getFormTable().getFieldBeanByDisplay("抄送-填写");
			if (null != csTFfb) {
				CAP4FormKitUtils.setCellValue(formMasterBean, "抄送-显示", null);
			}
			for (int i = 1; i <= 9; i++) {
				FormFieldBean spTFfb = formMasterBean.getFormTable().getFieldBeanByDisplay("审批" + i + "-职位");
				if (null == spTFfb)
					continue;
				CAP4FormKitUtils.setCellValue(formMasterBean, "审批" + i, null);
				CAP4FormKitUtils.setCellValue(formMasterBean, "审批" + i + "-姓名", null);
				CAP4FormKitUtils.setCellValue(formMasterBean, "审批" + i + "-职位", null);
				CAP4FormKitUtils.setCellValue(formMasterBean, "审批" + i + "-日期", null);
				CAP4FormKitUtils.setCellValue(formMasterBean, "审批" + i + "-签名", null);
			}
			CAP4FormKitUtils.setCellValue(formMasterBean, "处理部门", null);
			CAP4FormKitUtils.setCellValue(formMasterBean, "处理人", null);
			CAP4FormKitUtils.setCellValue(formMasterBean, "处理日期", null);
			CAP4FormKitUtils.setCellValue(formMasterBean, "处理态度", null);
			CAP4FormKitUtils.setCellValue(formMasterBean, "审批流程", null);
			formManager.saveOrUpdateFormData(formMasterBean, formBean.getId(), true);
			Map<String, Object> map = Maps.newHashMap();
			map.put("code", "200");
			response.getWriter().print(JSONUtil.toJSONString(map));
		} catch (BusinessException e) {
			log.error("locknlock BusinessException error:", e);
			System.out.println(e);
		} catch (SQLException e) {
			log.error("locknlock SQLException error:", e);
			System.out.println(e);
		} catch (IOException e) {
			log.error("locknlock IOException error:", e);
			System.out.println(e);
		}
	}

}
